﻿Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Resources



Public NotInheritable Class cDatabase
    Implements IDisposable

    Private _disposedValue As Boolean ' To detect redundant calls

    Private _LocRM As ResourceManager
    Private _ConnectionString As String
    Private _oConexion As SqlConnection
    Private _Transaction As SqlTransaction
    Private _Command As SqlCommand


    
    Public Property Conexion() As SqlConnection
        Get
            Return _oConexion
        End Get
        Set(value As SqlConnection)
            _oConexion = value
        End Set
    End Property

    Public Sub Conectar()
        _oConexion = New SqlConnection(_ConnectionString)
        If _oConexion.State = ConnectionState.Closed Then
            _oConexion.Open()
        End If
    End Sub

    Public Sub Desconectar()
        If _oConexion.State <> ConnectionState.Closed Then
            _oConexion.Close()
        End If
    End Sub

    Public Sub BeginTransaction()
        If _Transaction Is Nothing Then
            _oConexion.Open()
            _Transaction = _oConexion.BeginTransaction
            _Command.Transaction = _Transaction
        End If

    End Sub

    Public Sub RollbackTransaction()
        If _Transaction IsNot Nothing Then
            _Transaction.Rollback()
            _Transaction = Nothing
        End If
    End Sub

    Public Sub CommitTransaction()
        If _Transaction IsNot Nothing Then
            _Transaction.Commit()
            _Transaction = Nothing
        End If
    End Sub

    Private Sub New()
        'Dim strConnectionString = My.Settings.ConnString
        Dim strConnectionString = Configuration.ConfigurationManager.ConnectionStrings("UI.My.MySettings.Recruiting").ConnectionString

        _ConnectionString = strConnectionString
    End Sub

    Public Shared Function Instance() As cDatabase
        Return New cDatabase
    End Function

    Protected Sub Dispose(disposing As Boolean)
        If Not Me._disposedValue Then
            If disposing Then
                _oConexion.Close()
                _oConexion.Dispose()
            End If

            _LocRM = Nothing
        End If
        Me._disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

End Class
